/**
 * 
 */
package com.movitech.mobile.cz.base.common.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 * 
 * @author ThinkGem
 * @version 2014-4-15
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

	private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm",
			"yyyy.MM" };

	public static String SHORT_DATE = "yyyy-MM-dd";
	public static String SHORT_MD_DATE = "MM.dd";
	public static String SHORT_MD_DATE_CHN = "MM月dd日";
	public static String SHORT_MD_DATE_I = "MMdd";

	public static final String ZERO_DATE_TIME = "yyyy-MM-dd 00:00:00";
	public static final String DATE_TIME = "yyyy-MM-dd HH:mm:ss";
	public static final String SHORT_DATE_TIME = "yyyy-MM-dd HH:mm";
	public static final String SHORT_DATE_TIME_H = "yyyy-MM-dd HH";
	public static final String TIME = "HH:mm:ss";
	public static final String DATE_CN = "yyyy年MM月dd日";
	public static final String TIME_STAMP = "yyyyMMddHHmmssSSS";
	public static final String DATE_STAMP = "yyyyMMdd";
	public static final String DATE_XIE = "dd/MM";
	public static final String MONTH_XIE = "MM/yyyy";
	public static final String WEEK_XIE = "ww/yyyy";
    public static final String YMDHMS_FORMATE2= "yyyyMMddHHmmss";
	private static final Map<String, SimpleDateFormat> simpleFormatterMap = new HashMap<String, SimpleDateFormat>();

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd）
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}

    public static String getNowString() {
        return getDate(YMDHMS_FORMATE2);
    }
    
	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String getDate(String pattern) {
		return DateFormatUtils.format(DateUtils.getNow(), pattern);
	}

	public static Date getMinuterPlus(Date dateVal, int minutes) {
		if (dateVal == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();

		cal.setTime(dateVal);
		cal.add(Calendar.MINUTE, minutes);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static Date addMonth(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		cld.add(Calendar.MONTH, 1);
		return  cld.getTime();
	}
	
	/****
	 * 获取当前时间
	 * 
	 * @return
	 */
	public static Date getNow() {
		Calendar cal = Calendar.getInstance();
		
		return cal.getTime();
	}

	/****
	 * 获取当前时间
	 * 
	 * @return
	 */
	public static Long getNowLong() {
		Calendar cal = Calendar.getInstance();
		
		return cal.getTimeInMillis();
	}
	

	/****
	 * 获取当前时间
	 * 
	 * @return
	 */
	public static Date getNowDate(Long longDate) {
		if(longDate == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(longDate);
		return cal.getTime();
	}

	/**
	 * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length > 0) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}

	/**
	 * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date) {
		String formatDate = DateFormatUtils.format(date, DATE_TIME);
		return formatDate;
	}

	/**
	 * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String formatDateTime(Date date) {
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前时间字符串 格式（HH:mm:ss）
	 */
	public static String getTime() {
		return formatDate(DateUtils.getNow(), "HH:mm:ss");
	}

	/**
	 * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String getDateTime() {
		return formatDate(DateUtils.getNow(), "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前年份字符串 格式（yyyy）
	 */
	public static String getYear() {
		return formatDate(DateUtils.getNow(), "yyyy");
	}

	/**
	 * 得到当前月份字符串 格式（MM）
	 */
	public static String getMonth() {
		return formatDate(DateUtils.getNow(), "MM");
	}

	/**
	 * 得到当天字符串 格式（dd）
	 */
	public static String getDay() {
		return formatDate(DateUtils.getNow(), "dd");
	}

	/**
	 * 得到当前星期字符串 格式（E）星期几
	 */
	public static String getWeek() {
		return formatDate(DateUtils.getNow(), "E");
	}

	/**
	 * 日期型字符串转化为日期 格式 { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
	 * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy.MM.dd",
	 * "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
	 */
	public static Date parseDate(Object str) {
		if (str == null) {
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date parseLong(Long dateLong) {
		if (dateLong == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		//
		cal.setTimeInMillis(dateLong);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}


	public static Date parseLong2(Long dateLong) {
		if (dateLong == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();		
		//
		cal.setTimeInMillis(dateLong);
		return cal.getTime();
	}
	
	/**
	 * 获取过去的天数
	 * 
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = DateUtils.getNow().getTime() - date.getTime();
		return t / (24 * 60 * 60 * 1000);
	}

	/**
	 * 获取过去的小时
	 * 
	 * @param date
	 * @return
	 */
	public static long pastHour(Date date) {
		long t = DateUtils.getNow().getTime() - date.getTime();
		return t / (60 * 60 * 1000);
	}

	/**
	 * 获取过去的分钟
	 * 
	 * @param date
	 * @return
	 */
	public static long pastMinutes(Date date) {
		long t = DateUtils.getNow().getTime() - date.getTime();
		return t / (60 * 1000);
	}

	/**
	 * 转换为时间（天,时:分:秒.毫秒）
	 * 
	 * @param timeMillis
	 * @return
	 */
	public static String formatDateTime(long timeMillis) {
		long day = timeMillis / (24 * 60 * 60 * 1000);
		long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
		long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
		return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
	}

	/**
	 * 获取两个日期之间的天数
	 * 
	 * @param before
	 * @param after
	 * @return
	 */
	public static double getDistanceOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
	}

	public static Long getDayLong(Date dateVal) {
		if (dateVal == null) {
			return 0l;
		}
		Calendar cal = Calendar.getInstance();
		
		cal.setTime(dateVal);
		return cal.getTimeInMillis();
	}

	public static Date getDayFirst(Date dateVal) {
		if (dateVal == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		
		cal.setTime(dateVal);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	public static Long getDayLongFirst(Date dateVal) {
		if (dateVal == null) {
			return 0l;
		}
		Calendar cal = Calendar.getInstance();
		
		cal.setTime(dateVal);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTimeInMillis();
	}

	public static Date getDayEnd(Date dateVal) {
		if (dateVal == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		
		cal.setTime(dateVal);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}

	public static Long getNowDayEnd() {
		Calendar cal = Calendar.getInstance();
		
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTimeInMillis();
	}

	public static Date getDayPlus(Date dateVal, int days) {
		if (dateVal == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		
		cal.setTime(dateVal);
		cal.add(Calendar.DAY_OF_MONTH, days);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		cal.add(Calendar.MILLISECOND, -1);
		return cal.getTime();
	}

	public static Date getDayPlus2(Date dateVal, int days) {
		if (dateVal == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		
		cal.setTime(dateVal);
		cal.add(Calendar.DAY_OF_MONTH, days);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	

	public static Date getDayPlus( int days) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, days);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}


	/***
	 * 月份的加减
	 * @param dateVal
	 * @param months
	 * @return
	 */
	public static Date getMonthPlus(Date dateVal, int months) {
		if (dateVal == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		
		cal.setTime(dateVal);
		cal.add(Calendar.MONTH, months);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/***
	 * 年份的加减
	 * @param years
	 * @return
	 */
	public static Date getYearPlus(int years) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.YEAR, years);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	/***
	 * 年份的加减和月份第一天
	 * @param dateVal
	 * @param months
	 * @return
	 */
	public static Date getYearAndMonthFirstPlus(Date dateVal, int years) {
		if (dateVal == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		
		cal.setTime(dateVal);
		cal.add(Calendar.YEAR, years);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/***
	 * 递归获取当前日期所在的7天的首尾日期
	 * 
	 * @param useProdDate
	 * @param currentDate
	 * @return
	 */
	public static Map<String, Date> calculateServenDate(Date useProdDate, Date currentDate) {
		Map<String, Date> mapDate = new HashMap<String, Date>();
		if (DateUtils.getDayLong(DateUtils.getDayFirst(useProdDate)).longValue() <= DateUtils.getDayLong(DateUtils.getDayFirst(currentDate))) {
			System.out.println("============currentDate===========" + DateUtils.formatDate(currentDate, "yyyy-MM-dd HH:mm:ss"));
			System.out.println("============useProdDate===========" + DateUtils.formatDate(useProdDate, "yyyy-MM-dd HH:mm:ss"));
			Date startDate = DateUtils.getDayFirst(useProdDate);
			Date endDate = DateUtils.getDayPlus(useProdDate, 7);

			System.out.println("=======================" + DateUtils.formatDate(startDate, "yyyy-MM-dd HH:mm:ss"));
			System.out.println("=======================" + DateUtils.formatDate(endDate, "yyyy-MM-dd HH:mm:ss"));

			if (DateUtils.getDayLong(startDate).longValue() <= DateUtils.getDayLong(currentDate) && DateUtils.getDayLong(currentDate) <= DateUtils.getDayLong(endDate).longValue()) {
				mapDate.put("STARTDATE", startDate);
				mapDate.put("ENDDATE", endDate);
				return mapDate;
			} else {
				useProdDate = DateUtils.getDayPlus2(startDate, 7);
				System.out.println("============useProdDate=======1====" + DateUtils.formatDate(useProdDate, "yyyy-MM-dd HH:mm:ss"));
				useProdDate = DateUtils.getDayFirst(useProdDate);
				System.out.println("============useProdDate======2=====" + DateUtils.formatDate(useProdDate, "yyyy-MM-dd HH:mm:ss"));
			}
			return calculateServenDate(useProdDate, currentDate);
		}
		return mapDate;
	}

	public static List<Map<String, Date>> calculateServenDateRange(Date currentDate, List<Map<String, Date>> lstDateRangeMap) {
		if (lstDateRangeMap == null || (lstDateRangeMap != null && lstDateRangeMap.size() == 0))
			lstDateRangeMap = new ArrayList<Map<String, Date>>();
		Map<String, Date> mapDate = new HashMap<String, Date>();
		if (currentDate != null) {
			System.out.println("============currentDate===========" + DateUtils.formatDate(currentDate, "yyyy-MM-dd HH:mm:ss"));
			Date startDate = DateUtils.getDayFirst(currentDate);
			Date endDate = DateUtils.getDayPlus(currentDate, 7);
			System.out.println("=======================" + DateUtils.formatDate(startDate, "yyyy-MM-dd HH:mm:ss"));
			System.out.println("=======================" + DateUtils.formatDate(endDate, "yyyy-MM-dd HH:mm:ss"));
			mapDate.put("STARTDATE", startDate);
			mapDate.put("ENDDATE", endDate);
			lstDateRangeMap.add(mapDate);

			Long nowLong = DateUtils.getNowLong();
			if (DateUtils.getDayLong(startDate).longValue() <= nowLong && nowLong <= DateUtils.getDayLong(endDate).longValue()) {
				return lstDateRangeMap;
			} else {
				currentDate = DateUtils.getDayPlus2(startDate, 7);
				System.out.println("============currentDate=======1====" + DateUtils.formatDate(currentDate, "yyyy-MM-dd HH:mm:ss"));
				currentDate = DateUtils.getDayFirst(currentDate);
				System.out.println("============currentDate======2=====" + DateUtils.formatDate(currentDate, "yyyy-MM-dd HH:mm:ss"));
				return calculateServenDateRange(currentDate, lstDateRangeMap);
			}
		}
		return lstDateRangeMap;
	}

	public static boolean isWeekend(Calendar calendar) {
		if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			return true;
		}
		return false;
	}

	public static boolean isWeekend(Date date) {
		return isWeekend(DateUtils.toCalendar(date));
	}

	public static Date dateTop(Date datetime) {
		String date = format(datetime, "yyyy-MM-dd");
		return parse(date + " 00:00:00 000", "yyyy-MM-dd HH:mm:ss SSS");
	}

	public static Date dateBottom(Date datetime) {
		String date = format(datetime, "yyyy-MM-dd");
		return parse(date + " 23:59:59 999", "yyyy-MM-dd HH:mm:ss SSS");
	}

	public static boolean isSameDay(Calendar calendar1, Calendar calendar2) {
		return DateUtils.isSameDay(calendar1, calendar2);
	}

	public static boolean isSameDay(Date date1, Date date2) {
		return DateUtils.isSameDay(date1, date2);
	}

	public static boolean isSameWeek(Calendar calendar1, Calendar calendar2) {
		if (calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) && calendar1.get(Calendar.WEEK_OF_YEAR) == calendar2.get(Calendar.WEEK_OF_YEAR)) {
			return true;
		}
		return false;
	}

	public static boolean isSameWeek(Date date1, Date date2) {
		Calendar calendar1 = DateUtils.toCalendar(date1);
		Calendar calendar2 = DateUtils.toCalendar(date2);
		return isSameWeek(calendar1, calendar2);
	}

	public static String dateAdd(String flyDate, int day) {
		try {
			SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyy-MM-dd");
			Date date = simpleFormatter.parse(flyDate);
			Calendar calendar = Calendar.getInstance();
			
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_MONTH, day);
			return simpleFormatter.format(calendar.getTime());
		} catch (Exception e) {
		}
		return null;
	}

	public static Date dateAdd(Date date, int day) {
		try {
			Calendar calendar = Calendar.getInstance();
			
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_MONTH, day);
			return calendar.getTime();
		} catch (Exception e) {
		}
		return null;
	}

	public static boolean isSameMonth(Calendar calendar1, Calendar calendar2) {
		if (calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)) {
			return true;
		}
		return false;
	}

	public static boolean isSameMonth(Date date1, Date date2) {
		Calendar calendar1 = DateUtils.toCalendar(date1);
		Calendar calendar2 = DateUtils.toCalendar(date2);
		return isSameMonth(calendar1, calendar2);
	}

	/**
	 * 日期相差多少天
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int subDate(Date date1, Date date2) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			date1 = sdf.parse(sdf.format(date1));
			date2 = sdf.parse(sdf.format(date2));
			Calendar cal = Calendar.getInstance();
			
			cal.setTime(date1);
			long time1 = cal.getTimeInMillis();
			cal.setTime(date2);
			long time2 = cal.getTimeInMillis();
			long between_days = (time2 - time1) / (1000 * 3600 * 24);
			return Integer.parseInt(String.valueOf(between_days));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 以begin作为一周第一天，根据now获取now这周的第一天
	 * 
	 * @param begin
	 * @param now
	 * @return
	 */
	public static Date getNearWeek(Date begin, Date now) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			begin = sdf.parse(sdf.format(begin));
			now = sdf.parse(sdf.format(now));
			Calendar cal = Calendar.getInstance();
			
			cal.setTime(begin);
			long time1 = cal.getTimeInMillis();
			cal.setTime(now);
			long time2 = cal.getTimeInMillis();
			long between_days = (time2 - time1) / (1000 * 3600 * 24) % 7;
			int day = 0 - Integer.parseInt(String.valueOf(between_days));
			String near = dateAdd(format(now, SHORT_DATE), day);
			return parse(near, SHORT_DATE);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date parse(String date, String model) {
		Date retVal = null;
		SimpleDateFormat simpleFormatter = simpleFormatterMap.get(model);
		if (simpleFormatter == null) {
			simpleFormatter = new SimpleDateFormat(model);
			simpleFormatterMap.put(model, simpleFormatter);
		}
		try {
			retVal = simpleFormatter.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return retVal;
	}

	public static String format(Date date, String model) {
		SimpleDateFormat simpleFormatter = simpleFormatterMap.get(model);
		if (simpleFormatter == null) {
			simpleFormatter = new SimpleDateFormat(model);
			simpleFormatterMap.put(model, simpleFormatter);
		}
		return simpleFormatter.format(date);
	}

	public static String format(String flyDate, String flyTime) {

		try {
			SimpleDateFormat simpleFormatter = new SimpleDateFormat(DATE_TIME);
			SimpleDateFormat simpleFormatter0 = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat simpleFormatter1 = new SimpleDateFormat("yyyy-MM-ddHHmm");
			flyDate = simpleFormatter0.format(simpleFormatter0.parse(flyDate));
			Date date = simpleFormatter1.parse(flyDate + flyTime);
			return simpleFormatter.format(date);
		} catch (Exception e) {
		}
		return null;
	}
	
	/**
	 * 根据用户生日计算年龄
	 */
	public static int getAgeByBirthday(Date now,Date birthday) {
		if(now==null||birthday==null){
			return 0;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		if (cal.before(birthday)) {
			return 0;
		}
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

		cal.setTime(birthday);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

		int age = yearNow - yearBirth;

		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				// monthNow==monthBirth 
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}
			} else {
				// monthNow>monthBirth 
				age--;
			}
		}
		return age;
	}

	/**
	 * 比较日期相差的田硕
	 * @param fDate
	 * @param oDate
	 * @return
	 */
	public static int getIntervalDays(Date fDate, Date oDate) {
		Calendar aCalendar = Calendar.getInstance();
		aCalendar.setTime(fDate);
		int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
		aCalendar.setTime(oDate);
		int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
		return day2 - day1;
	}
	
	/****
	 * 获取当前在本月的第几天
	 * @return
	 */
	public static int getCurrentDay() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/****
	 * 获取当天的几点
	 * @return
	 */
	public static int getCurrentHours() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.HOUR_OF_DAY);
	}

	public static int getCurrentHours(Date dateVal) {
		if (dateVal == null) {
			return -1;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateVal);
		return cal.get(Calendar.HOUR_OF_DAY);
	}
	
	
	/**
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
		 System.out.println(format(DateUtils.getMonthPlus(DateUtils.getNow(),-1),"yyyy-MM-dd HH:mm:ss"));
		 
		 
		 
		// Date d=DateUtils.getNow();
		//
		// System.out.println(format(d,DATE_TIME));
		//
		// System.out.println(formatDate(parseDate("2010/3/6")));
		// System.out.println(getDate("yyyy年MM月dd日 E"));
		// long time = DateUtils.getNow().getTime()-parseDate("2012-11-19").getTime();
		// System.out.println(time/(24*60*60*1000));
	}
}
